<?php

declare (strict_types=1);

namespace app\model;


use app\BaseModel;
use think\model\concern\SoftDelete;


class SystemDeptModel extends BaseModel
{
    use SoftDelete;

    protected $autoWriteTimestamp = true;
    //软删除
    protected $deleteTime = 'delete_time';
    protected $hidden = ['delete_time'];
    protected $name = 'system_dept';


    public function generalTree($res): array
    {
        foreach ($res as &$v) {
            $result = self::where('parent_id', $v['id'])->select()->toArray();
            if ( ! empty($result)) {
                $result = $this->generalTree($result);
            }
            $v['children'] = $result;
        }

        return $res;
    }


    public function getAllChildren($arr): array
    {
        $data = $arr;
        foreach ($arr as $v) {
            $children = self::where('parent_id', $v)->column('id');
            if ( ! empty($children)) {
                $data = array_merge($data, $this->getAllChildren($children));
            }
        }

        return $data;
    }
}